//
//  BankListVC.swift
//  HengYouCai
//
//  Created by lwj on 2017/12/15.
//  Copyright © 2017年 lwj. All rights reserved.
//

import UIKit
import SnapKit

//MARK:___Cell___
class BankListCell: UITableViewCell {
    
    
    
    //配置cell
    func configurationCell(_ modelTemp: BankModel, _ listView: UIScrollView, _ indexPath: IndexPath) {
        
        if let url = URL(string: modelTemp.bankImage) {
//            bankImg.kf.setImage(with: ImageResource(downloadURL: url))
            bankImg.kf_setImage(with: ImageResource(downloadURL: url))

        }
        bankName.text = modelTemp.bankName
        bankDetail.text = modelTemp.bankLimitTips
        if modelTemp.recommendBankShow == "1" {
            recommendImg.image = UIImage(named: "recommendBank")
        }else{
            recommendImg.image = nil;
            
        }
        self.contentView.backgroundColor = modelTemp.bankStatus == "1" ? ColorFromRGBHex_WL(0xf2f2f2) : UIColor.white
        self.isUserInteractionEnabled = modelTemp.bankStatus != "1"
    }
    //快捷生成cell
    static func wl_createCellWithTableView(_ tableView: UITableView) -> BankListCell {
        
        let cellID =
            NSStringFromClass(BankListCell.self);
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? BankListCell {
            return cell
        }else{
            return BankListCell(style: .default, reuseIdentifier: cellID)
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       
        self.selectionStyle = .none
        initUI()
    }
    func initUI() {
        
        self.contentView.addSubview(bankImg)
        _ = bankImg.sd_layout()
            .centerYEqualToView(self.contentView)?
            .leftSpaceToView(self.contentView,15)?
            .heightIs(30)?
            .widthIs(30)
        
        self.contentView.addSubview(bankName)
        _ = bankName.sd_layout()
            .topSpaceToView(self.contentView,11)?
            .leftSpaceToView(bankImg,19)?
            .heightIs(15)?
            .widthIs(250)
        self.contentView.addSubview(bankDetail)
        _ = bankDetail.sd_layout()
            .bottomSpaceToView(self.contentView,11)?
            .leftSpaceToView(bankImg,19)?
            .heightIs(12)?
            .widthIs(250)
        
        self.contentView.addSubview(recommendImg)
        recommendImg.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
        }

    }
    /// 银行图片
    lazy var bankImg: UIImageView = {
        let temp = UIImageView()
        return temp
    }()
    
    /// 银行名字
    lazy var bankName: UILabel = {
        let temp = UILabel()
        temp.font = Font_WL(14)
        temp.textColor = Normal_333Color
        temp.textAlignment = .left
        
        return temp
    }()
    /// 银行金额限制
    lazy var bankDetail: UILabel = {
        let temp = UILabel()
        temp.font = Font_WL(11)
        temp.textColor = Normal_666Color
        temp.textAlignment = .left
        temp.text = "单笔限额50万，单日限额200万"
        return temp
    }()
    
    /// 推荐使用图片
    lazy var recommendImg: UIImageView = {
        let temp = UIImageView()
        return temp
    }()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
}
class BankModel: BaseObject {
    
    var bankStatus = ""
    /// 银行名字
    var bankName = ""
    
    /// 银行id
    var bankId = ""
    
    /// 银行公告
    var bankTips  = ""
    
    /// 银行图片
    var bankImage = ""
    var bankLimitTips = ""
    /// 是否推荐使用
    var recommendBankShow = ""

}

class BankListVC: BaseViewController,UITableViewDelegate,UITableViewDataSource,WCarouselViewDelegate {
    func numberOfItems(inCarouseView carouseView: WCarouselView!) -> Int {
        return tipsArr.count
    }
    
    func carouseView(_ carouseView: WCarouselView!, indexOfItem index: Int) -> UIView! {
        
        let textLab = UILabel()
        textLab.font = Font_WL(12)
        textLab.textColor = Normal_RedColor
        
        textLab.text = tipsArr[index]
        
        return textLab
        
    }
    
    /// tableView的代理协议
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bankLists!.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = BankListCell.wl_createCellWithTableView(tableView)
        cell.configurationCell(bankLists![indexPath.row], tableView, indexPath)
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.navigationController?.popViewController(animated: true)
        self.callBack?(self.bankLists![indexPath.row])
    }

    deinit {
        
        for subView in self.view.subviews {
            debugPrint(subView)
            subView.removeFromSuperview()
        }
        debugPrint("1111111")
    }
    
    /// 银行卡列表
    var bankLists : Array<BankModel>?
    
    /// 公告数组
    var tipsArr : Array<String> = []
    
    var myTableView : UITableView?
    
    ///回调函数
    var callBack: ((_ model : BankModel)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
    
        view.backgroundColor = UIColor.white
        self.navigationItem.title = "选择银行"
        if #available(iOS 11.0, *) {
            
            self.myTableView?.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
       
      initTableView()
    }

    
    /// 初始化tableView
    func initTableView(){
        //
        
        if tipsArr.count > 0 {
            addTipsView()
        }
        let tableViewY : CGFloat = tipsArr.count > 0 ? 23 + kJcwGetNavHeight()  : 0 + kJcwGetNavHeight()

        let headerView = UIView();
        
        headerView.frame = CGRect(x: 0, y: tableViewY, width: JCWSCREENW, height: 30)
        headerView.backgroundColor = Normal_458Color;
        self.view.addSubview(headerView)
        let tishiLab = UILabel()
        tishiLab.frame = CGRect(x: 15, y: 0, width: JCWSCREENW-30, height: 30)
        tishiLab.text = "仅支持储蓄卡、暂不支持信用卡、具体金额以实际限额为准";
        tishiLab.numberOfLines = 0;
        tishiLab.textColor = Normal_999Color;
        tishiLab.font = Font_WL(12)
        tishiLab.backgroundColor = UIColor.clear
        headerView.addSubview(tishiLab)
        
        let rect = CGRect(x: 0, y: tableViewY+30, width: JCWSCREENW, height: JCWSCREENH - tableViewY - 30 - SafeAreaInsetsWithBottom())
        
        myTableView = UITableView(frame: rect, style: .plain)
        myTableView?.separatorColor = Normal_CCCColor
        myTableView?.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        myTableView?.rowHeight = 55
        
        myTableView?.delegate = self
        myTableView?.dataSource = self
        view.addSubview(myTableView!)
        for bankModel:BankModel in self.bankLists ?? [] {
            if bankModel.bankTips.count > 0{
                self.tipsArr.append(bankModel.bankTips)
            }
        }
         myTableView?.reloadData()
    }
    func addTipsView() {
        let tipsView = UIView()
        tipsView.backgroundColor = ColorFromRGBHex_WL(0xffe8bd)
        tipsView.frame = CGRect(x: 0, y: kJcwGetNavHeight(), width: JCWSCREENW, height: 23)
        view.addSubview(tipsView)
        
        let leftImg = UIImageView(image: UIImage(named: "bank_inform"))
        tipsView.addSubview(leftImg)
        _ = leftImg.sd_layout().centerYEqualToView(tipsView)?.leftSpaceToView(tipsView,15)?.heightIs(12.5)?.widthIs(12.5)
        
        let temp = WCarouselView()
        temp.scrollingDirection = WScrollingDirectionVertical
        temp.backgroundColor = UIColor.clear
        temp.isContinuous = true
        temp.autoPlayTime = 2.0
        temp.delegate = self
        tipsView.addSubview(temp)
        _ = temp.sd_layout()
            .topSpaceToView(tipsView,0)?
            .leftSpaceToView(leftImg,5)?
            .rightSpaceToView(tipsView,10)?
            .bottomSpaceToView(tipsView,0)
        
        
        
    }
//    /// 数据请求
//    func loadDate() {
//        //
//
//        self.view.showHUDLoading()
//        HYCNetService.accountActionBankList(onSuccess: { (result) in
//
//            self.view.hideHUDLoading(0)
//            self.bankLists = BankModel.mj_objectArray(withKeyValuesArray: result?["bankList"]) as! Array<BankModel>
//            for bankModel:BankModel in self.bankLists {
//                if bankModel.bankTips.count > 0{
//                    self.tipsArr.append(bankModel.bankTips)
//                }
//            }
//            self.initTableView()
//        }) { (code, errorMsg) in
//            //
//           self.view.showHUDFailTips(errorMsg, hideDelay: kHUDHideDelay)
//        }
//    }
//
}
